package com.littcore.saap.assistant.web;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.littcore.common.Utility;
import com.littcore.exception.NotLoginException;
import com.littcore.saap.assistant.service.IUserPortletService;
import com.littcore.saap.assistant.vo.UserPortletVo;
import com.littcore.saap.common.vo.LoginUserVo;
import com.littcore.saap.core.module.portal.PortletMeta;
import com.littcore.saap.core.module.portal.PortletMetaManager;
import com.littcore.saap.core.module.portal.PortletMetaParam;
import com.littcore.saap.core.web.util.LoginUtils;

@Controller
public class UserPortletController {

	@Resource
	private IUserPortletService userPortletService;
	
	@RequestMapping 
  public ModelAndView index(WebRequest request, ModelMap modelMap) throws NotLoginException
  { 
    LoginUserVo loginUserVo = (LoginUserVo)LoginUtils.getLoginVo();
    
    List<UserPortletVo> userPortlets = userPortletService.findByUser(LoginUtils.getLoginOpId().intValue());
    
    //return params to response
    modelMap.addAttribute("userPortlets", userPortlets);  
    
    return new ModelAndView("/assistant/portal/index");  
  
  }   
	  
  @RequestMapping 
  public ModelAndView add(WebRequest request)
  { 
    List<PortletMeta> portletMetas = PortletMetaManager.getInstance().getAllPortletMeta();
    
    return new ModelAndView("/assistant/portal/add").addObject("portletMetas", portletMetas);  
  } 
  
  @RequestMapping 
  public ModelAndView save(WebRequest request)
  { 
    String portletUid = request.getParameter("uid");
    int userId = LoginUtils.getLoginOpId().intValue();
    String name = request.getParameter("name");
    int refreshInterval = Utility.parseInt(request.getParameter("refreshInterval"));
    int positionX = Utility.parseInt(request.getParameter("positionX"));
    int positionY = Utility.parseInt(request.getParameter("positionY"));    
    
    PortletMeta portletMeta = PortletMetaManager.getInstance().getPortletMeta(portletUid);    
    Map<String, Object> params = new HashMap<String, Object>();
    List<PortletMetaParam> metaParams = portletMeta.getParams();
    for (PortletMetaParam portletMetaParam : metaParams)
    {
      String key = portletMetaParam.getName();
      params.put(key, request.getParameter(key));
    }
    
    userPortletService.save(portletUid, userId, name, refreshInterval, positionX, positionY, params);
    return new ModelAndView("/assistant/portal/add");  
  } 
  
  @RequestMapping 
  public void updateName(@RequestParam Integer id, @RequestParam(required=false) String name)
  { 
    userPortletService.updateName(id, name);
  }
	  
	@RequestMapping 
	public void delete(@RequestParam Integer id)
	{ 
	  userPortletService.delete(id);
	}
	
	@RequestMapping 
  public ModelAndView getMeta(@RequestParam String uid)
  { 
	  PortletMeta portletMeta = PortletMetaManager.getInstance().getPortletMeta(uid);
	  return new ModelAndView("jsonView").addObject("portletMeta", portletMeta);
  }
	
}
